<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>更新商品</title>
		<script type="text/javascript" src="/js/jquery-1.11.1.js"></script>
		<script type="text/javascript" src="/js/bootstrap.js"></script>
		<link rel="stylesheet" type="text/css" href="/css/bootstrap.css"/>
		<link rel="stylesheet" type="text/css" href="/css/style.css"/>
		<style>
			label span{color: #ff0000; padding-right: 5px;}
		</style>
	</head>

	<body>
		<h2>更新商品</h2>
		<form id="myForm" class="form-horizontal">
			<!--springmvc的restful风格处理非get post方式请求必备参数-->
			<!--<input name="_method"  type="hidden" value="PUT"/>-->
			<div class="form-group">
				<label class="col-sm-1 control-label"><span>*</span>商品序号</label>
				<div class="col-sm-9">
					<input name="p_num"  type="text" disabled/>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-1 control-label"><span>*</span>商品编号</label>
				<div class="col-sm-9">
					<input required="required" name="p_id" class="form-control" />
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-1 control-label"><span>*</span>商品名称</label>
				<div class="col-sm-9">
					<input required="" name="p_name" class="form-control" />
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-1 control-label">商品类型</label>
				<div class="col-sm-9">
					<input name="p_type" class="form-control" />
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-1 control-label">商品品牌</label>
				<div class="col-sm-9">
					<input name="p_brand" class="form-control" />
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-1 control-label">条形码</label>
				<div class="col-sm-9">
					<input name="p_code" class="form-control" />
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-1 control-label">规格</label>
				<div class="col-sm-9">
					<input name="p_rule" class="form-control"/>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-1 control-label">成本价</label>
				<div class="col-sm-9">
					<input name="p_cost" class="form-control"/>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-1 control-label">零售价</label>
				<div class="col-sm-9">
					<input name="p_retail" class="form-control"/>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-1 control-label">备注</label>
				<div class="col-sm-9">
					<input name="p_remarks" class="form-control"/>
				</div>
			</div>

			<div class="form-group">
				<label class="col-sm-1 control-label"></label>
				<div class="col-sm-9">
					<button id="btnUpdate" type="button" class="btn btn-sm btn-info">提&nbsp;交</button>
					&nbsp;&nbsp;&nbsp;&nbsp;
					<button type="reset"  class="btn btn-sm btn-warning">重&nbsp;置</button>
				</div>
			</div>
		</form>		
	</body>
</html>
<script>
	String.prototype.GetValue= function(para) {
		let reg = new RegExp("(^|&)"+ para +"=([^&]*)(&|$)");
		let r = this.substr(this.indexOf("\?")+1).match(reg);
		if (r!=null) return unescape(r[2]); return null;
	}
	$(function () {
		//根据地址获取teamId,如果null则执行增加，否则执行更新
		let url = document.location.toString();//获取URL
		let p_num=url.GetValue("p_num");
		//alert(teamId);
		//根据id查询要更新的球队信息并回显到页面
		queryById(p_num);
		//提交按钮的单击事件--实现真正的更新
		$("#btnUpdate").click(function () {
			$.ajax({
				type: "POST",//这里必须写POST
				url: "/product/"+p_num,
				data: $("#myForm").serialize()+"&_method=PUT",
				dataType:"json",
				success: function(vo){
					if(vo.code==200) {
						alert("操作成功！");
						window.location.href = "/pages/product/list.html";
					}else{
						alert("更新失败！"+vo.msg);
					}
				}
			});
		});
	});
	function queryById(p_num) {
		$.ajax({
			type: "GET",
			url: "/product/"+p_num, //RESTful风格的API定义
			data: "",
			success: function (vo) {
				let obj=vo.obj;
				let p_num=obj.p_num;
				let p_id=obj.p_id;
				let p_name=obj.p_name;
				let p_type=obj.p_type==null?'':obj.p_type;
				let p_brand=obj.p_brand==null?'':obj.p_brand;
				let p_code=obj.p_code==null?'':obj.p_code;
				let p_rule=obj.p_rule==null?'':obj.p_rule;
				let p_cost=obj.p_cost===0?0:obj.p_cost;
				let p_retail=obj.p_retail===0?0:obj.p_retail;
				let p_remarks=obj.p_remarks==null?'':obj.p_remarks;
				$("input[name='p_num']").val(p_num);
				$("input[name='p_id']").val(p_id);
				$("input[name='p_name']").val(p_name);
				$("input[name='p_type']").val(p_type);
				$("input[name='p_brand']").val(p_brand);
				$("input[name='p_code']").val(p_code);
				$("input[name='p_rule']").val(p_rule);
				$("input[name='p_cost']").val(p_cost);
				$("input[name='p_retail']").val(p_retail);
				$("input[name='p_remarks']").val(p_remarks);
			}
		});
	}

</script>